package com.Exam;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class Exam01 {
   // 使用阻塞队列实现消费者生产者问题
   public static void main(String[] args) {
      // 容量是1  put方法执行一次 就开始等待了  take 取一次就开始等待
      BlockingQueue<String> bq = new ArrayBlockingQueue<>(1);
      CookerThread lgw = new CookerThread("李国伟", bq);
      CookerThread xdj = new CookerThread("小当家", bq);
      CookerThread dc = new CookerThread("大厨", bq);
      EaterThread zyj = new EaterThread("张一健", bq);
      EaterThread lyy = new EaterThread("懒羊羊", bq);
      lgw.start();
      xdj.start();
      dc.start();
      zyj.start();
      lyy.start();
   }
}


class CookerThread extends Thread {

   private BlockingQueue<String> bq;

   public CookerThread(String name, BlockingQueue<String> bq) {
      super(name);
      this.bq = bq;
   }

   public void run() {
      while (true) {
         synchronized (CookerThread.class) {
            String name = String.valueOf(System.currentTimeMillis());
            try {
               bq.put(name);
               System.out.println(Thread.currentThread().getName() + "做了一个老八秘制小汉堡：" + name);
               Thread.sleep(1000);
            } catch (InterruptedException e) {
               break;
            }
         }
      }
   }
}
class EaterThread extends Thread {

   private BlockingQueue<String> bq;

   public EaterThread(String name, BlockingQueue<String> bq) {
      super(name);
      this.bq = bq;
   }

   public void run() {
      while (true) {
         synchronized (this) {
            try {
               String take = bq.take();
               System.out.println(getName() + "吃了一个老八秘制小汉堡：" + take);
               Thread.sleep(1000);
            } catch (InterruptedException e) {
               break;
            }
         }
      }
   }
}